package com.zhou.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zhou.constant.MessageConstant;
import com.zhou.dao.MemberMapper;
import com.zhou.dao.OrderMapper;
import com.zhou.dao.OrderSettingMapper;
import com.zhou.entity.Result;
import com.zhou.pojo.Member;
import com.zhou.pojo.Order;
import com.zhou.pojo.OrderSetting;
import com.zhou.service.OrderService;
import com.zhou.utils.DateUtils;
import com.zhou.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 添加预约订单
     *
     * @param map
     * @return
     * @throws Exception
     */
    public Result order(Map map) throws Exception {
        //TODO 0、判断预约的地址是否是正确的  返回的公司地址对应的id
//        int addressId;
//        try {
//            addressId = mapMapper.findCountByAddressName((String) map.get("companyAddress"));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.SELECTED_ADDRESS_FAIL);
//        }
        //1检查当前日期是否进行了预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations >= number) {
            //已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3检查用户是否是重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByTelephone(telephone);
        if (member != null) {
            Integer memberId = member.getId();
            Date date1 = DateUtils.parseString2Date(orderDate);
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(memberId, date, Integer.parseInt(setmealId));
            //根据条件进行查询
            List<Order> list = orderMapper.findByCondition(order);
            if (list != null && list.size() > 0) {
                //说明用户在重复预约，无法完成再次预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //4检查当前用户是否为会员，根据手机号判断
            member = new Member();
            member.setFileNumber(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));//档案号
            member.setName((String) map.get("name"));
            member.setPassword(MD5Utils.md5("123456"));//设置默认密码123456
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setBirthday(DateUtils.parseString2Date((String) map.get("birthday")));
            member.setRegTime(new Date());
            memberMapper.add(member);//自动完成会员注册
        }
        //5可以预约，设置预约人数加一
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        //TODO 地址
//        order.setAddressId(addressId);
        orderMapper.add(order);
        //保存预约信息到预约表
        orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
        orderSettingMapper.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    //根据id查询预约信息，包括体检人信息、套餐信息
    public Map findById(Integer id) throws Exception {
        Map map = orderMapper.findById4Detail(id);
        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
